package com.ruoyi.jksd.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.jksd.common.JksdUtils;
import com.ruoyi.jksd.common.dto.XyRechargeDto;
import com.ruoyi.jksd.domain.XyBalanceLog;
import com.ruoyi.jksd.domain.XyUsers;
import com.ruoyi.jksd.service.IXyUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jksd.mapper.XyRechargeMapper;
import com.ruoyi.jksd.domain.XyRecharge;
import com.ruoyi.jksd.service.IXyRechargeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员-充值Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@Service
public class XyRechargeServiceImpl implements IXyRechargeService {
    @Autowired
    private XyRechargeMapper xyRechargeMapper;

    @Autowired
    private IXyUsersService xyUsersService;

    /**
     * 查询会员-充值
     *
     * @param id 会员-充值主键
     * @return 会员-充值
     */
    @Override
    public XyRecharge selectXyRechargeById(String id) {
        return xyRechargeMapper.selectXyRechargeById(id);
    }

    /**
     * 查询会员-充值列表
     *
     * @param xyRecharge 会员-充值
     * @return 会员-充值
     */
    @Override
    public List<XyRecharge> selectXyRechargeList(XyRecharge xyRecharge) {
        return xyRechargeMapper.selectXyRechargeList(xyRecharge);
    }

    /**
     * 新增会员-充值
     *
     * @param xyRecharge 会员-充值
     * @return 结果
     */
    @Override
    public int insertXyRecharge(XyRecharge xyRecharge) {
        return xyRechargeMapper.insertXyRecharge(xyRecharge);
    }

    /**
     * 修改会员-充值
     *
     * @param xyRecharge 会员-充值
     * @return 结果
     */
    @Override
    public int updateXyRecharge(XyRecharge xyRecharge) {
        return xyRechargeMapper.updateXyRecharge(xyRecharge);
    }

    /**
     * 批量删除会员-充值
     *
     * @param ids 需要删除的会员-充值主键
     * @return 结果
     */
    @Override
    public int deleteXyRechargeByIds(String[] ids) {
        return xyRechargeMapper.deleteXyRechargeByIds(ids);
    }

    /**
     * 删除会员-充值信息
     *
     * @param id 会员-充值主键
     * @return 结果
     */
    @Override
    public int deleteXyRechargeById(String id) {
        return xyRechargeMapper.deleteXyRechargeById(id);
    }

    @Override
    public AjaxResult recharge(XyRechargeDto xyRechargeDto) {
        Long uid = xyRechargeDto.getUid();
        XyUsers xyUsers = xyUsersService.selectXyUsersById(uid + "");
        if (null == xyUsers) {
            return AjaxResult.error(201, "UID ERROR");
        }
        String rechargeSn = JksdUtils.buildSn("SY");
//        'id' => $id,
//                'uid' => $uid,
//                'tel' => $uinfo['tel'],
//                'real_name' => $uinfo['username'],
//                'pic' => $pic,
//                'num' => $num,
//                'addtime' => time(),
//                'pay_name' => $type
        XyRecharge xyRecharge = new XyRecharge();
        xyRecharge.setId(rechargeSn);
        xyRecharge.setUid(uid);
        xyRecharge.setRealName(xyUsers.getRealName());
        xyRecharge.setPic(xyRechargeDto.getPic());
        xyRecharge.setNum(xyRechargeDto.getNum());
        xyRecharge.setAddtime(System.currentTimeMillis()/1000);
        xyRecharge.setPayName("USDT");

        XyBalanceLog balanceLog = new XyBalanceLog();
        balanceLog.setUid(xyUsers.getId());
        balanceLog.setOid(xyRecharge.getId());
        balanceLog.setNum(xyRecharge.getNum());
        balanceLog.setStatus(1L);
        balanceLog.setAddtime(System.currentTimeMillis()/1000);
        balanceLog.setType(1L);//充值

        xyRechargeMapper.insertXyRecharge(xyRecharge);
        return AjaxResult.success(xyRecharge);
    }

    /**
     * 审批充值
     *
     * @param xyRecharge
     * @return
     */
    @Override
    @Transactional
    public AjaxResult approveRecharge(XyRecharge xyRecharge) {
        Long uid = xyRecharge.getUid();
        XyUsers xyUsers = xyUsersService.selectXyUsersById(uid + "");
        xyRecharge.setEndtime(System.currentTimeMillis()/1000);
        xyRechargeMapper.updateXyRecharge(xyRecharge);
//        @Excel(name = "订单状态 1下单成功 2充值成功 3充值失败")
        if (xyRecharge.getStatus() == 3) {
            return AjaxResult.success();
        } else if (2 == xyRecharge.getStatus()) {
            if (null == xyUsers) {
                return AjaxResult.error(201, "UID ERROR");
            }
            BigDecimal newBalance = xyUsers.getBalance().add(xyRecharge.getNum());
            xyUsers.setBalance(newBalance);
            xyUsersService.updateXyUsers(xyUsers);
        } else {
            AjaxResult.error("params error");
        }


        return AjaxResult.success();
    }
}
